#include <QGridLayout>
#include <QLabel>
#include <QSlider>
#include <QSpacerItem>
#include "configuration.h"
#include "../controls/colorbutton.h"
#include "../drawingsurface.h"
#include "../global.h"

namespace ShUi
{

class ConfigurationImpl : public Impl<Configuration>
{
public:
    void init();
    void exit();
    ColorButton* _penColor;
    ColorButton* _bgColor;
    QSlider* _penWidth;
};

void ConfigurationImpl::init()
{
    SH_Q(Configuration);
    QGridLayout* layout = new QGridLayout();
    // pen color
    _penColor = new ColorButton(VgPenColor);
    layout->addWidget(new QLabel(q->tr("pen color")), 0, 0);
    layout->addWidget(_penColor, 0, 1);

    // background color
    _bgColor = new ColorButton(VgBgColor);
    layout->addWidget(new QLabel(q->tr("background color")), 1, 0);
    layout->addWidget(_bgColor, 1, 1);
    
    layout->addItem(new QSpacerItem(20, 20), 2, 0);
    // pen width
    _penWidth = new QSlider(Qt::Horizontal);
    _penWidth->setRange(1, 8);
    _penWidth->setValue(VgPenWidth);
    layout->addWidget(new QLabel(q->tr("pen width")), 3, 0);
    layout->addWidget(_penWidth, 3, 1);

    q->setLayout(layout);
}

void ConfigurationImpl::exit()
{
}

Configuration::Configuration(QWidget *parent) :
    QDialog(parent)
{
    SH_I(Configuration);
    d->init();
    // connections
    connect(d->_penColor, SIGNAL(colorChanged(const QColor&)),
            this, SLOT(setPenColor(const QColor&)));
    connect(d->_bgColor, SIGNAL(colorChanged(const QColor&)),
            this, SLOT(setBgColor(const QColor&)));
    connect(d->_penWidth, SIGNAL(valueChanged(int)),
            this, SLOT(setPenWidth(int)));
}

Configuration::~Configuration()
{
    SH_E(Configuration);
    d->exit();
}

void Configuration::setBgColor(const QColor& color)
{
    SH_D(Configuration);
    if (VgBgColor != color)
    {
        VgBgColor = color;
        d->_bgColor->setColor(color);
        DrawingSurface* surface = getSurface();
        if (surface)
            surface->setBgColor(color);
    }
}

void Configuration::setPenColor(const QColor& color)
{
    SH_D(Configuration);
    if (VgPenColor != color)
    {
        VgPenColor = color;
        d->_penColor->setColor(color);
        DrawingSurface* surface = getSurface();
        if (surface)
            surface->setPenColor(color);
    }
}

void Configuration::setPenWidth(int width)
{
    SH_D(Configuration);
    if (VgPenWidth != width)
    {
        VgPenWidth = width;
        d->_penWidth->setValue(width);
        DrawingSurface* surface = getSurface();
        if (surface)
            surface->setPenWidth(width);
    }
}

} // namespace ShUi
